﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Live;

namespace ShareFolder
{
    public class GClass5 : IDisposable
    {
        private LiveConnectClient liveConnectClient_0;
        private List<GClass2> list_0;
        private string string_0;
        private AutoResetEvent autoResetEvent_0 = new AutoResetEvent(false);
        private EventHandler<GEventArgs0> eventHandler_0;
        private EventHandler eventHandler_1;
        private EventHandler eventHandler_2;
        
        private static Func<GClass2, bool> func_0;
        
        private static Func<GClass2, string> func_1;
        
        private static Func<GClass2, bool> func_2;
        
        private static Func<GClass2, string> func_3;
        public void method_0(EventHandler<GEventArgs0> eventHandler_3)
        {
            EventHandler<GEventArgs0> eventHandler = this.eventHandler_0;
            EventHandler<GEventArgs0> eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler<GEventArgs0> eventHandler3 = (EventHandler<GEventArgs0>)Delegate.Combine(eventHandler2, eventHandler_3);
                eventHandler = Interlocked.CompareExchange<EventHandler<GEventArgs0>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
            }
            while (eventHandler != eventHandler2);
        }
        public void method_1(EventHandler<GEventArgs0> eventHandler_3)
        {
            EventHandler<GEventArgs0> eventHandler = this.eventHandler_0;
            EventHandler<GEventArgs0> eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler<GEventArgs0> eventHandler3 = (EventHandler<GEventArgs0>)Delegate.Remove(eventHandler2, eventHandler_3);
                eventHandler = Interlocked.CompareExchange<EventHandler<GEventArgs0>>(ref this.eventHandler_0, eventHandler3, eventHandler2);
            }
            while (eventHandler != eventHandler2);
        }
        public void method_2(EventHandler eventHandler_3)
        {
            EventHandler eventHandler = this.eventHandler_1;
            EventHandler eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler eventHandler3 = (EventHandler)Delegate.Combine(eventHandler2, eventHandler_3);
                eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_1, eventHandler3, eventHandler2);
            }
            while (eventHandler != eventHandler2);
        }
        public void method_3(EventHandler eventHandler_3)
        {
            EventHandler eventHandler = this.eventHandler_1;
            EventHandler eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler eventHandler3 = (EventHandler)Delegate.Remove(eventHandler2, eventHandler_3);
                eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_1, eventHandler3, eventHandler2);
            }
            while (eventHandler != eventHandler2);
        }
        public void method_4(EventHandler eventHandler_3)
        {
            EventHandler eventHandler = this.eventHandler_2;
            EventHandler eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler eventHandler3 = (EventHandler)Delegate.Combine(eventHandler2, eventHandler_3);
                eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_2, eventHandler3, eventHandler2);
            }
            while (eventHandler != eventHandler2);
        }
        public void method_5(EventHandler eventHandler_3)
        {
            EventHandler eventHandler = this.eventHandler_2;
            EventHandler eventHandler2;
            do
            {
                eventHandler2 = eventHandler;
                EventHandler eventHandler3 = (EventHandler)Delegate.Remove(eventHandler2, eventHandler_3);
                eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.eventHandler_2, eventHandler3, eventHandler2);
            }
            while (eventHandler != eventHandler2);
        }
        public string method_6()
        {
            return this.string_0;
        }
        public LiveConnectClient method_7()
        {
            return this.liveConnectClient_0;
        }
        public GClass5(LiveConnectSession liveConnectSession_0)
        {
            this.liveConnectClient_0 = new LiveConnectClient(liveConnectSession_0);
            this.liveConnectClient_0.GetCompleted += new EventHandler<LiveOperationCompletedEventArgs>(this.method_10);
            this.liveConnectClient_0.PostCompleted += new EventHandler<LiveOperationCompletedEventArgs>(this.method_12);
            this.liveConnectClient_0.UploadCompleted += new EventHandler<LiveOperationCompletedEventArgs>(this.method_15);
            this.liveConnectClient_0.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(this.method_14);
            this.liveConnectClient_0.PutCompleted += new EventHandler<LiveOperationCompletedEventArgs>(this.method_18);
            this.liveConnectClient_0.DeleteCompleted += new EventHandler<LiveOperationCompletedEventArgs>(this.method_20);
        }
        public void method_8()
        {
            this.autoResetEvent_0.Set();
        }
        public void method_9(string string_1)
        {
            this.liveConnectClient_0.GetAsync(string_1);
        }
        private void method_10(object sender, LiveOperationCompletedEventArgs e)
        {
            List<GClass2> list = new List<GClass2>();
            if (e.Error == null)
            {
                Dictionary<string, object> dictionary = (Dictionary<string, object>)e.Result;
                List<object> list2 = (List<object>)dictionary["data"];
                this.list_0 = new List<GClass2>();
                using (List<object>.Enumerator enumerator = list2.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        object current = enumerator.Current;
                        Dictionary<string, object> dictionary2 = (Dictionary<string, object>)current;
                        GClass2 gClass = new GClass2();
                        gClass.Id = dictionary2["id"].ToString();
                        gClass.FileName = dictionary2["name"].ToString();
                        if (!(dictionary2["type"].ToString() == "folder") && !(dictionary2["type"].ToString() == "album"))
                        {
                            gClass.ImageSrc = "/Images/file.png";
                            if (dictionary2.ContainsKey("size"))
                            {
                                gClass.FileSize = Convert.ToInt32(dictionary2["size"]);
                            }
                        }
                        else
                        {
                            gClass.ImageSrc = "/Images/folder.png";
                            gClass.IsDirectory = true;
                        }
                        this.list_0.Add(gClass);
                    }
                }
                List<GClass2> arg_18C_0 = list;
                IEnumerable<GClass2> arg_165_0 = this.list_0;
                if (GClass5.func_0 == null)
                {
                    GClass5.func_0 = new Func<GClass2, bool>(GClass5.smethod_0);
                }
                IEnumerable<GClass2> arg_187_0 = Enumerable.Where<GClass2>(arg_165_0, GClass5.func_0);
                if (GClass5.func_1 == null)
                {
                    GClass5.func_1 = new Func<GClass2, string>(GClass5.smethod_1);
                }
                arg_18C_0.AddRange(Enumerable.OrderBy<GClass2, string>(arg_187_0, GClass5.func_1));
                List<GClass2> arg_1DC_0 = list;
                IEnumerable<GClass2> arg_1B5_0 = this.list_0;
                if (GClass5.func_2 == null)
                {
                    GClass5.func_2 = new Func<GClass2, bool>(GClass5.smethod_2);
                }
                IEnumerable<GClass2> arg_1D7_0 = Enumerable.Where<GClass2>(arg_1B5_0, GClass5.func_2);
                if (GClass5.func_3 == null)
                {
                    GClass5.func_3 = new Func<GClass2, string>(GClass5.smethod_3);
                }
                arg_1DC_0.AddRange(Enumerable.OrderBy<GClass2, string>(arg_1D7_0, GClass5.func_3));
                EventHandler<GEventArgs0> arg_1F9_0 = this.eventHandler_0;
                GEventArgs0 gEventArgs = new GEventArgs0();
                gEventArgs.method_1(list);
                arg_1F9_0.Invoke(this, gEventArgs);
                return;
            }
            this.eventHandler_0.Invoke(this, new GEventArgs0
            {
                string_0 = e.Error.Message
            });
        }
        public void method_11(string string_1, string string_2)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            this.string_0 = null;
            dictionary.Add("name", string_2);
            this.liveConnectClient_0.PostAsync(string_1, dictionary);
        }
        private void method_12(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.string_0 = e.Error.Message;
            }
            this.eventHandler_1.Invoke(this, new EventArgs());
        }
        public void method_13(string string_1, string string_2, Stream stream_0)
        {
            this.liveConnectClient_0.UploadAsync(string_1, string_2, stream_0, OverwriteOption.Rename);
            this.autoResetEvent_0.WaitOne();
            if (!string.IsNullOrEmpty(this.string_0))
            {
                throw new Exception(this.string_0);
            }
        }
        private void method_14(object sender, LiveDownloadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.string_0 = e.Error.Message;
            }
            else
            {
                MemoryStream memoryStream = e.Result as MemoryStream;
                Stream stream = e.UserState as Stream;
                memoryStream.WriteTo(stream);
            }
            this.autoResetEvent_0.Set();
        }
        private void method_15(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.string_0 = e.Error.Message;
            }
            this.autoResetEvent_0.Set();
        }
        public void method_16(string string_1, Stream stream_0)
        {
            this.string_0 = null;
            this.liveConnectClient_0.DownloadAsync(string_1, stream_0);
            this.autoResetEvent_0.WaitOne();
            if (!string.IsNullOrEmpty(this.string_0))
            {
                throw new Exception(this.string_0);
            }
        }
        public void method_17(string string_1, string string_2)
        {
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("name", string_2);
            this.string_0 = null;
            this.liveConnectClient_0.PutAsync(string_1, dictionary);
        }
        private void method_18(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.string_0 = e.Error.Message;
            }
            this.eventHandler_2.Invoke(this, new EventArgs());
        }
        public void method_19(string string_1)
        {
            this.string_0 = null;
            this.liveConnectClient_0.DeleteAsync(string_1);
            this.autoResetEvent_0.WaitOne();
            if (!string.IsNullOrEmpty(this.string_0))
            {
                throw new Exception(this.string_0);
            }
        }
        private void method_20(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                this.string_0 = e.Error.Message;
            }
            this.autoResetEvent_0.Set();
        }
        public void Dispose()
        {
            this.autoResetEvent_0.Close();
            this.liveConnectClient_0 = null;
        }
        
        private static bool smethod_0(GClass2 gclass2_0)
        {
            return gclass2_0.IsDirectory;
        }
        
        private static string smethod_1(GClass2 gclass2_0)
        {
            return gclass2_0.FileName;
        }
        
        private static bool smethod_2(GClass2 gclass2_0)
        {
            return !gclass2_0.IsDirectory;
        }
        
        private static string smethod_3(GClass2 gclass2_0)
        {
            return gclass2_0.FileName;
        }
    }
}
